home *** CD-ROM | disk | FTP | other *** search
/ MacAddict 116 / MacAddict 116 (Mac Power Pack)(theDISC)(April 2006).iso / Software / Interface / HolidayWidgetBundle.dmg / / VoiceNotes.wdgt / voicenotes.js < prev   
Encoding:
Text File  |  2005-06-15  |  28.7 KB  |  1,070 lines

  1. var old_note=null, cur_note=null, num_notes = 0; //to be set in init() and changed by add/delete
  2. var notes_folder = "~/Documents/VoiceNotes/";
  3. var adding_note = false;
  4.  
  5. function getNotes() {
  6.     var list = widget.system("/bin/ls " + notes_folder, null).outputString;
  7.  
  8.     if (list !== undefined) {
  9.         var notes = list.split("\n"); //this isn't going to scale well for a huge amount of notes - will need to rehtink this......
  10.         
  11.         num_notes = notes.length-1;
  12.     
  13.         
  14.         for (var i=0; i < num_notes; i++) {
  15.             var notes_list = document.getElementById("notes");
  16.         
  17.             var item = document.createElement("li");
  18.             item.innerText = notes[i].substring(0, notes[i].lastIndexOf("."));
  19.             item.setAttribute("note", notes_folder + notes[i]);
  20.             
  21.             notes_list.appendChild(item);
  22.         }
  23.         
  24.         cur_note = 0;
  25.         selectNote(0);
  26.     }
  27. }
  28.  
  29. function selectNote(note) {
  30.     var notes = document.getElementById("notes");
  31.     
  32.     cur_note = note;
  33.     
  34.     var all_notes = notes.getElementsByTagName("li");
  35.  
  36.     if (old_note != null) {
  37.         all_notes[old_note].style.background = "none";
  38.         all_notes[old_note].style.color = "";
  39.     }
  40.  
  41.     all_notes[cur_note].style.background = "#1E2E8B";
  42.     all_notes[cur_note].style.color = "#95BDEA";
  43.     
  44.     old_note = cur_note;
  45.     
  46.     toggleButtons("play"); //incase they're playing another note...
  47. }
  48.  
  49. function xButtonHandler() {
  50.     if (atMenu) {
  51.         deleteNote();
  52.     } else {
  53.         if (!isPaused) pauseNote();
  54.         shuntScreens('right', 'play', function(){atMenu=true;});
  55.     }
  56. }
  57.  
  58. function deleteNote() {
  59.     if (num_notes > 0) {
  60.         if (!adding_note) {
  61.             //trash the file, rather than actually removing it:
  62.             widget.system("/bin/mv " + getUnixFilePath(document.getElementById("notes").getElementsByTagName("li")[cur_note].getAttribute("note")) + " ~/.Trash/", null);
  63.         } else {
  64.             document.getElementById("edit_div").style.display = "none";
  65.             adding_note = false;
  66.         }
  67.             
  68.         document.getElementById("notes").removeChild(document.getElementById("notes").getElementsByTagName("li")[cur_note]);
  69.         old_note = null;
  70.         num_notes--;
  71.     
  72.         if (cur_note != null) {
  73.             if (cur_note >= num_notes) cur_note--;
  74.             
  75.             if (cur_note >= 4) {
  76.                 document.getElementById("notes").style.top = -14*(cur_note-4) + "px";
  77.             }
  78.                 
  79.             if (cur_note >= 0) selectNote(cur_note);
  80.         }
  81.         
  82.         setUpScrollBar();
  83.         toggleButtons("play"); //just incase ;)
  84.     }
  85. }
  86.  
  87. function addNote() {
  88.     if (!adding_note) {
  89.         //event.preventDefault();
  90.         //event.stopPropagation();
  91.         adding_note = true;
  92.         
  93.         var notes = document.getElementById("notes");
  94.         
  95.         notes.style.top = 0;
  96.     
  97.         cur_note = 0;
  98.         
  99.         var item = document.createElement("li");
  100.         item.innerHTML = " "; //just sets the height, is all... shap u!
  101.         
  102.         if (notes.getElementsByTagName("li").length > 0) 
  103.             notes.insertBefore(item, notes.getElementsByTagName("li")[cur_note]);
  104.         else 
  105.             notes.appendChild(item);
  106.     
  107.         if (old_note != null) old_note++;
  108.         num_notes++;
  109.         cur_note = 0;
  110.         selectNote(cur_note);
  111.         
  112.         setUpScrollBar();
  113.     
  114.         var edit_div = document.getElementById("edit_div");
  115.         edit_div.innerText = "New Note";
  116.         edit_div.style.display = "block";
  117.         
  118.         /*
  119.             the following is just putting the caret at the end
  120.             of edit div so that the text can be edited :)
  121.             Found this stuff in the KHTML source...
  122.          */
  123.         
  124.         setTimeout(
  125.             function () {
  126.                 var range = document.createRange();
  127.                 range.selectNodeContents(edit_div);
  128.                 
  129.                 var sel=window.getSelection();
  130.                 sel.setBaseAndExtent(edit_div.firstChild, 0, edit_div.lastChild, edit_div.lastChild.length);
  131.                 //sel.collapseToEnd();
  132.             
  133.                 toggleButtons("record");
  134.                 //setUpScrollBar();
  135.             }, 100);
  136.     }
  137. }
  138.  
  139. var recording_timer, show_time_elapsed=true;
  140.  
  141. function recordNewNote() {
  142.     if (VoiceNotes) {
  143.         var note_name = document.getElementById("edit_div").innerText;
  144.         document.getElementById("edit_div").style.display = "none";
  145.         
  146.         document.getElementById("notes").getElementsByTagName("li")[cur_note].innerText = note_name;
  147.         document.getElementById("notes").getElementsByTagName("li")[cur_note].setAttribute("note", notes_folder + note_name + ".m4a");
  148.         
  149.         toggleButtons("stop");
  150.         
  151.         document.getElementById("recording_title").innerText = "Recording";
  152.         document.getElementById("recording_name").innerText = note_name;
  153.         document.getElementById("cur_rec_time").innerText = "00:00";
  154.         
  155.         shuntScreens('left', 'record', 
  156.             function() {
  157.                 alert(notes_folder + note_name + ".m4a");
  158.                 VoiceNotes.recordToFile(notes_folder + note_name + ".m4a");
  159.                 var cur_rec_time = 0;                
  160.                 recording_timer = setInterval(
  161.                     function () {
  162.                         cur_rec_time += 600;
  163.                         document.getElementById("cur_rec_time").innerText = formatTime(cur_rec_time);
  164.                     }, 1000);
  165.             });
  166.     }
  167. }
  168.  
  169. function stopIt() {
  170.     if (VoiceNotes) {
  171.         clearInterval(recording_timer);
  172.         document.getElementById("recording_title").innerText = "Saving";
  173.  
  174.         document.getElementById("async").style.display = "block";
  175.         anim.play('forward', null);
  176.         
  177.         setTimeout( //doing this so the async has a chance to spin... 
  178.             function() {
  179.                 VoiceNotes.stopRecording(); //needs to be threaded or sopething because it stops the async anim when run
  180.                 toggleButtons("play");
  181.  
  182.                 selectNote(0);
  183.                 
  184.                 anim.stop();
  185.                 document.getElementById("async").style.display = "none";
  186.                         
  187.                 shuntScreens('right', 'record', null);
  188.                 adding_note = false;
  189.                 
  190.             }, 500);
  191.     }
  192. }
  193.  
  194. var end_time, isPaused = false, atMenu = true;
  195.  
  196. function playNote() {    
  197.     document.getElementById("now_playing").innerText = document.getElementById("notes").getElementsByTagName("li")[cur_note].innerText;
  198.     document.getElementById("cur_time").innerText = "00:00";
  199.  
  200.     toggleButtons("pause");
  201.     document.getElementById("pause").src = "images/pause_normal.png"; //in case it's still on play
  202.  
  203.     shuntScreens('left', 'play', 
  204.         function() {
  205.         //alert("file://" + encodeURI(document.getElementById("notes").getElementsByTagName("li")[cur_note].getAttribute("note")));
  206.             document.note_player.SetURL("file://" + encodeURI(document.getElementById("notes").getElementsByTagName("li")[cur_note].getAttribute("note")));
  207.             document.note_player.Play();
  208.             isPaused = false;
  209.             
  210.             alert('yo - now playing');
  211.             
  212.             
  213.             document.getElementById("outside_cur_time").style.display = "block";
  214.             
  215.             end_time = document.note_player.GetEndTime();
  216.             
  217.             atMenu = false;
  218.             
  219.             play();
  220.         });
  221. }
  222.  
  223. var play_interval;
  224.  
  225. function play() {
  226.     play_interval = setInterval(
  227.         function() {
  228.             var cur_time = document.note_player.GetTime();
  229.             
  230.             if (cur_time < end_time) {
  231.                 document.getElementById("cur_time").innerText = formatTime(show_time_elapsed ? cur_time : (end_time - cur_time));
  232.                 document.getElementById("progress_bar").style.width = ((cur_time/end_time)* 100) + "%";
  233.             } else {
  234.                 clearInterval(play_interval);
  235.                 toggleButtons("play");
  236.                 document.getElementById("progress_bar").style.width = 0;
  237.                 document.getElementById("outside_cur_time").style.display = "none";
  238.                 shuntScreens('right', 'play', null);
  239.                 atMenu = true;
  240.             }
  241.         }, 1000);
  242. }
  243.  
  244. function pauseNote() {    
  245.     var pause_button = document.getElementById("pause");
  246.     
  247.     if (!isPaused) {
  248.         document.note_player.Stop();
  249.         clearInterval(play_interval);
  250.         pause_button.src = "images/play_normal.png";
  251.         document.getElementById("outside_cur_time").style.display = "none";
  252.         //shuntScreens('right', 'play', null);
  253.     } else {
  254.         document.note_player.Play();
  255.         play();
  256.         pause_button.src = "images/pause_normal.png";
  257.         document.getElementById("outside_cur_time").style.display = "block";
  258.         if (atMenu) {
  259.             shuntScreens('left', 'play', null);
  260.             toggleButtons("pause");    
  261.         }
  262.         atMenu = false;
  263.     }
  264.     
  265.     isPaused = !isPaused;
  266. }
  267.  
  268. function switchTime() {
  269.     show_time_elapsed=!show_time_elapsed
  270.     
  271.     var cur_time = document.note_player.GetTime();
  272.     
  273.     if (show_time_elapsed) {
  274.         document.getElementById("cur_time").innerText = formatTime(cur_time);
  275.         document.getElementById("time_type").innerText = "Time Elapsed: ";
  276.     } else {
  277.         document.getElementById("cur_time").innerText = formatTime((end_time - cur_time));                
  278.         document.getElementById("time_type").innerText = "Remaining Time: ";
  279.     }
  280. }
  281.  
  282. function setTime(e) {
  283.     //get percentage across they clicked:
  284.     var perc = ((e.clientX - e.target.offsetLeft) / e.target.offsetWidth);
  285.     document.note_player.SetTime(end_time * perc);
  286. }
  287.  
  288. function formatTime(time) {
  289.     var units = Math.floor(time/(600)); //actually total seconds assuming a document.note_player.GetTimeScale() of 600
  290.     var minutes = Math.floor(units/(60));
  291.     var seconds = units%60;
  292.     
  293.     if (minutes < 10) minutes = "0" + minutes;
  294.     if (seconds < 10) seconds = "0" + seconds;
  295.     
  296.     return minutes + ":" + seconds;
  297. }
  298.  
  299. function shuntScreens(dir, which, callBack) {
  300.     var el = document.getElementById("screens");
  301.     if (which == "play") {
  302.         document.getElementById("recording_screen").style.display = "none";
  303.         document.getElementById("playing_screen").style.display = "inline-block";
  304.     } else {
  305.         document.getElementById("recording_screen").style.display = "inline-block";
  306.         document.getElementById("playing_screen").style.display = "none";
  307.     }
  308.     
  309.     var to, from;
  310.     
  311.     if (dir == "left") {
  312.         to = -130;
  313.         from = 0;
  314.     } else {
  315.         to = 0;
  316.         from = -130;
  317.     }
  318.     
  319.     var dif = -Math.abs(to - from);
  320.     
  321.     var pi = Math.PI;
  322.     var i = 0;
  323.     
  324.     var points = [];
  325.     
  326.     for (var theta=0; theta < 90; theta+=10) {
  327.         var step;
  328.         if (to > from) 
  329.             step = Math.cos((theta*pi)/180);
  330.         else 
  331.             step = Math.sin((theta*pi)/180);
  332.  
  333.         points.push(dif*step);                
  334.     }
  335.     
  336.     var inter = setInterval(
  337.         function() {
  338.             if (++i < points.length) {
  339.                 el.style.left = points[i] + "px";
  340.             } else {
  341.                 el.style.left = to + "px";
  342.                 clearInterval(inter);
  343.                 if (callBack != null) callBack();
  344.             }
  345.         }, 10);
  346. }
  347.  
  348. var isScrolling = false, scrollint = null;
  349.  
  350. function scrollUp() {
  351.     if (atMenu) {
  352.         if (--cur_note >= 0) {
  353.             selectNote(cur_note);
  354.         
  355.             if (cur_note >= 4) {
  356.                 document.getElementById("notes").style.top = -14*(cur_note-4) + "px";
  357.             }
  358.             
  359.             document.getElementById("up_arrow").style.visibility = (cur_note == 0) ? "hidden" : "visible";
  360.             document.getElementById("down_arrow").style.visibility = "visible";
  361.             
  362.             setUpScrollBar();
  363.         } else {
  364.             cur_note = 0;
  365.             cancelScroll();
  366.         }
  367.     }
  368. }
  369.  
  370. function scrollDown() {
  371.     if (atMenu) {
  372.         if (++cur_note < num_notes) {
  373.             selectNote(cur_note);
  374.     
  375.             if (cur_note > 4) {
  376.                 document.getElementById("notes").style.top = -14*(cur_note-4) + "px";
  377.             }
  378.     
  379.             document.getElementById("up_arrow").style.visibility = "visible";
  380.             document.getElementById("down_arrow").style.visibility = (cur_note == num_notes-1) ? "hidden" : "visible";
  381.             
  382.             setUpScrollBar();
  383.         } else {
  384.             cur_note = num_notes-1;
  385.             cancelScroll();
  386.         }
  387.     }
  388. }
  389.  
  390. function cancelScroll() {
  391.     isScrolling = false;
  392.     clearInterval(scrollint);
  393.     scrollint = null;
  394. }
  395.  
  396. function handleScroll(direction) {
  397.     if (atMenu) {
  398.         isScrolling = true;
  399.             
  400.         var dir = direction;
  401.         
  402.         setTimeout(
  403.             function() {
  404.                 if (isScrolling) {
  405.                     var scroller = (dir == "up") ? "scrollUp()" : "scrollDown()";
  406.                     if (scrollint == null) scrollint = setInterval(scroller, 100);
  407.                 }
  408.             }, 500);
  409.     }
  410. }
  411.  
  412. function setUpScrollBar() {
  413.     var scrollbar = document.getElementById("scroll_bar");
  414.     var notes_cont = document.getElementById("notes_container");
  415.     var notes = document.getElementById("notes");
  416.     var scroll_tab = document.getElementById("scroll_tab");
  417.     
  418.     var ratio = notes_cont.offsetHeight/notes.offsetHeight;
  419.     
  420.     if (ratio < 1) {
  421.         scroll_tab.style.top = (-1*ratio) * notes.offsetTop + 1 + "px";
  422.         scroll_tab.style.height = ratio*scrollbar.offsetHeight + "px";
  423.         scroll_tab.style.display = "block";
  424.     } else {
  425.         scroll_tab.style.display = "none";
  426.         
  427.     }
  428. }
  429.  
  430. //the following function traps the return key for adding a note, and
  431. function handleKeyPress(e) {
  432.     //alert(e.charCode);
  433.     
  434.     if (!help_running) {
  435.     //i'll find a better way of handling all this when I wake up :'(
  436.         if (e.charCode == 13 && e.target.id == "edit_div") {
  437.             //handle adding the note on return - maybe do an animation for the record button.
  438.             recordNewNote();
  439.             
  440.             e.preventDefault();
  441.             e.stopPropagation();
  442.         }
  443.         
  444.         //alert(e.keyCode);
  445.         if (e.keyCode == 38) {
  446.             scrollUp();
  447.             e.preventDefault();
  448.             e.stopPropagation();
  449.         }
  450.         
  451.         if (e.keyCode == 40) {
  452.             scrollDown();
  453.             e.preventDefault();
  454.             e.stopPropagation();
  455.         }
  456.         
  457.         if (e.keyCode == 32 && !adding_note) {
  458.             if (atMenu)
  459.                 playNote();
  460.             else
  461.                 pauseNote();
  462.                 
  463.             e.preventDefault();
  464.             e.stopPropagation();
  465.         }
  466.         
  467.     } else {
  468.         if (e.charCode == 27) 
  469.             cancelHelp();
  470.         else 
  471.             return false;
  472.     }
  473. }
  474.  
  475. function getUnixFilePath(file) {
  476.     if (file.indexOf("file://localhost") != -1) file = file.substring(new String("file://localhost").length, file.length);
  477.     //encode it so it doesn't matter if it's normal spaces or %20s
  478.     file = encodeURI(file);
  479.     file = file.replace(/%20/g,"\\ ");
  480.     file = decodeURI(file);
  481.     return file;
  482. }
  483.  
  484. function getFileName(file) {
  485.     var tmp = file.split("/");
  486.     
  487.     tmp = tmp[tmp.length-1];
  488.     tmp = tmp.replace(/\\ /g, " ");
  489.  
  490.     return tmp;
  491. }
  492.  
  493. function folderExists(path) {
  494.     var folders = path.split("/");
  495.     
  496.     var folder = folders[folders.length-2];
  497.     parent_folder = path.substring(0, path.indexOf("/" + folder));
  498.     
  499.     var list = widget.system("/bin/ls " + parent_folder, null).outputString;
  500.     
  501.     var regex = new RegExp("^" + folder + "$", "img");
  502.     
  503.     if (regex.exec(list)) return true;
  504.  
  505.     return false;
  506. }
  507.  
  508. function expandTilde(path) {
  509. //find a more full proof method for this
  510.  
  511.     if (path.charAt(0) == "~") {
  512.         path = path.substring(1, path.length);
  513.         
  514.         var username = widget.system("/usr/bin/whoami", null).outputString.split("\n")[0]; //since we get a newline character too
  515.         
  516.         var full_path = "/Users/" + username + path;    
  517.     } else {
  518.         return path;
  519.     }
  520.     
  521.     return full_path;
  522. }
  523.  
  524. function saveSettings() {
  525.     toggleSettings();
  526. }
  527.  
  528. function toggleButtons(which) {
  529.     var buts = document.getElementById("buttons").getElementsByTagName("img");
  530.     
  531.     for (var i=0; i < buts.length; i++) {
  532.         if (buts[i].className == "middle") {
  533.             buts[i].style.display = (buts[i].id == which) ? "block" : "none";
  534.         }
  535.     }
  536. }
  537.  
  538. function toggleSettings() {
  539.     var f = document.getElementById("front");
  540.     var p = document.getElementById("settings");
  541.                
  542.     if (f.style.display != "none") {
  543.         f.style.display = "none";
  544.         p.style.display = "block";
  545.         
  546.         var bod = document.getElementsByTagName("body")[0];
  547.         bod.removeEventListener("mouseover", mouseOver, true);
  548.         bod.removeEventListener("mouseout", mouseOut, true);
  549.         
  550.         //This should be done in init() but didn't seem to work......
  551.         //if they're registered, we get rid of the stuff on the back:
  552.         
  553.         if (VoiceNotes.isRegistered()) {
  554.             //alert('registered');
  555.             document.getElementById("settings").style["background-image"] = "url(images/backside_registered.png)";
  556.             document.getElementById("register_button").style.display = "none";
  557.             document.getElementById("buy_button").style.display = "none";
  558.         } else {
  559.             //alert('not registered yet');
  560.         }
  561.                 
  562.         if (window.widget) widget.prepareForTransition("ToBack");
  563.     } else {
  564.         p.style.display = "none";
  565.         f.style.display = "block";
  566.         
  567.         var bod = document.getElementsByTagName("body")[0];
  568.         bod.addEventListener("mouseover", mouseOver, true);
  569.         bod.addEventListener("mouseout", mouseOut, true);
  570.         
  571.         if (window.widget) widget.prepareForTransition("ToFront");
  572.     }
  573.     if (window.widget) setTimeout("widget.performTransition()", 0);
  574. }
  575.  
  576. function register() {
  577.     if (VoiceNotes) VoiceNotes.registerWidget();
  578. }
  579.  
  580. function buy() {
  581.     if (VoiceNotes) VoiceNotes.buyWidget();
  582. }
  583.  
  584. function done() {
  585.     toggleSettings();
  586. }
  587.  
  588. function Animation(id, what, strip_width, loops, interval) {
  589.     this.inter = null;
  590.     
  591.     this.instance = id; //i suppose this could possibly be done using random numbers instead, but...
  592.     eval(this.instance + " = this"); //giving us a reference to the object - I don't like eval in general, but this is an easy way of getting round this problem
  593.     
  594.     this.element = document.getElementById(what);
  595.     this.left = 0;
  596.     this.cur_loop = 0;
  597.     this.dx = 26; // no display so can't get offsetWidth this.element.offsetWidth + 10;
  598.     this.strip_width = strip_width;
  599.     this.loops = loops;
  600.     this.interval = interval;
  601.     this.isPlaying = false;
  602.     
  603.     this.end_point = 0;
  604.     
  605.     this.dir = "forward";
  606.     this.callBack = null;
  607.     
  608.     this.play = 
  609.         function(dir, callBack) {
  610.             if (!this.isPlaying) {
  611.                 this.isPlaying = true;
  612.                 this.dir = dir;
  613.                 this.callBack = callBack;
  614.                 
  615.                 if (this.dir == "back") {
  616.                     this.end_point =  this.strip_width * -1;
  617.                     this.left = 10;
  618.                 } else {
  619.                     this.end_point = 0;
  620.                     this.left = this.strip_width;
  621.                 }
  622.                 
  623.                 this.inter = setInterval(this.instance + ".roll()", this.interval);
  624.             }
  625.         }
  626.     
  627.     this.stop = 
  628.         function () {
  629.             clearInterval(this.inter);
  630.             this.isPlaying = false;
  631.         }
  632.         
  633.     this.roll =
  634.         function() {
  635.             this.left -= this.dx;
  636.             
  637.             if (this.left >= this.end_point) {
  638.                 var pos = (this.dir == "back") ? this.left * -1 : this.left;
  639.                 this.element.style.backgroundPosition = pos + "px 0";
  640.             } else {
  641.                 if (this.loops > 0 && ++this.cur_loop >= this.loops) {
  642.                     clearInterval(this.inter);
  643.                     this.cur_loop = 0;
  644.                     this.isPlaying = false;
  645.                     if (this.callBack != null) {
  646.                         this.callBack();
  647.                         this.callBack = null;
  648.                     }
  649.                 } else {
  650.                     this.element.style.backgroundPosition = "0 0";
  651.                     this.left = (this.dir == "back") ? 10 : this.strip_width;
  652.                 }
  653.             }
  654.         }
  655. }
  656.  
  657.  
  658. //mouseover stuff for the 'i' button:
  659.  
  660. var isShown = false, isAnimating = false;
  661.  
  662. function mouseOver(e) {
  663.     //e.cancelBubble = true;
  664.  
  665.     if (!isShown && !isAnimating) {
  666.     //alert('yo');
  667.     //document.getElementById("flip").style.display = "block";
  668.         fade('flipper', 0, 1, function(){isAnimating=false;isShown = true;});
  669.         isAnimating = true;
  670.     }
  671.     
  672.     if (e.target.parentNode.id == "flip" || e.target.id == "flip") {
  673.         document.getElementById("flip_back").style.visibility = "visible";
  674.     }
  675. }
  676.  
  677. function mouseOut(e) {
  678.     //e.cancelBubble = true;
  679.     
  680.     if (e.target.id != "flipper" && e.target.parentNode.id != "flip") {
  681.         document.getElementById("flip_back").style.visibility = "hidden";
  682.     }
  683.     
  684. //had a lot fo trouble with event capturing, so going this route for now...
  685. //we'll see what happens with actual Tiger...
  686.     if (e.clientY < 0 || e.clientX < 0 || e.clientY > 250 || e.clientX > 230) {
  687.         if (!isAnimating) {
  688.             fade('flipper', 1, 0, function(){isAnimating=false;isShown = false;});
  689.             isAnimating = true;
  690.         }
  691.     }
  692. }
  693.  
  694. function infoMouseUp(e) {
  695.     fade('flipper', 0, 0, function(){isAnimating=false;isShown = false;});
  696.     document.getElementById("flip_back").style.visibility = "hidden";
  697.     toggleSettings();
  698. }
  699.  
  700. function fade(el, from, to, callBack) {
  701.     if (el == new String(el)) el = document.getElementById(el);
  702.     //so we can pass a string for the id or an element
  703.       
  704.     var dif = Math.abs(to - from);
  705.     
  706.     var pi = Math.PI;
  707.     var i = 0;
  708.     
  709.     //if (to < from && el.style.opacity > 0) return;
  710.     
  711.     var points = [];
  712.  
  713.     for (var theta=0; theta<90; theta+=10) {
  714.         var step;
  715.         if (to > from) 
  716.             step = Math.sin((theta*pi)/180);
  717.         else 
  718.             step = Math.cos((theta*pi)/180);
  719.         
  720.         points.push(dif*step);
  721.     }
  722.     
  723.     var inter = setInterval(
  724.         function() {
  725.             if (++i < points.length) {
  726.                 el.style.opacity = Math.max(0.01, points[i]); //fixing safari glitch
  727.             } else {
  728.                 el.style.opacity = to;
  729.                 clearInterval(inter);
  730.                 if (callBack != null) callBack();
  731.             }
  732.         }, 100);
  733. }
  734.  
  735. //help stuff:
  736. var help_running = false;
  737.  
  738. function getOffsetTop(element) {
  739.     var top = 0;
  740.     var el = element;
  741.     
  742.     do {
  743.         top += el.offsetTop;
  744.         el = el.parentNode;
  745.     } while (el !== document.body);
  746.     
  747.     return top;
  748. }
  749.  
  750. function getOffsetLeft(element) {
  751.     var left = 0;
  752.     var el = element;
  753.     
  754.     do {
  755.         left += el.offsetLeft;
  756.         el = el.parentNode;
  757.     } while (el !== document.body);
  758.     
  759.     return left;
  760. }
  761.  
  762. var help_canvas_pen, help_canvas_x_stretch, help_canvas_y_stretch;
  763.  
  764. function showHelp() {
  765.     //this is basically an animation showing how to use the widget:
  766.     help_running = true;
  767.  
  768.     //HAHA - the following is almost a joke, isn't it?
  769.     
  770.     document.getElementById("cover").style.display = "block";
  771.     toggleSettings();
  772.     setTimeout(
  773.     function() {
  774.         showHelpText(0,
  775.         function () {
  776.             help_canvas_pen = 1;
  777.             help_canvas_y_stretch = 4;
  778.             help_canvas_x_stretch = 6;
  779.             circleObject('screen',
  780.             function () {
  781.                 setTimeout(
  782.                 function() {
  783.                     hideHelpText(0, 
  784.                     function () {
  785.                         setTimeout(
  786.                         function() {
  787.                             showHelpText(1,
  788.                             function () {
  789.                                 //help_canvas_pen = 3;
  790.                                 help_canvas_y_stretch = 1.5;
  791.                                 help_canvas_x_stretch = 4;
  792.                                 circleObject('up',
  793.                                 function () {
  794.                                     setTimeout(
  795.                                     function() {
  796.                                         hideHelpText(1, 
  797.                                         function () {
  798.                                             circleObject('down',
  799.                                                 function () {
  800.                                                 setTimeout(
  801.                                                 function() {
  802.                                                     help_canvas_y_stretch = 2;
  803.                                                     help_canvas_x_stretch = 2.5;
  804.                                                     circleObject('play',
  805.                                                     function () {
  806.                                                         showHelpText(2,
  807.                                                         function () {
  808.                                                             setTimeout(
  809.                                                             function() {
  810.                                                                 hideHelpText(2, 
  811.                                                                 function () {
  812.                                                                     showHelpText(3,
  813.                                                                     function () {
  814.                                                                         help_canvas_y_stretch = 1.5;
  815.                                                                         help_canvas_x_stretch = 2;
  816.                                                                         circleObject('delete',
  817.                                                                         function() {
  818.                                                                             setTimeout(
  819.                                                                             function() {
  820.                                                                                 hideHelpText(3, 
  821.                                                                                 function () {
  822.                                                                                     circleObject('new',
  823.                                                                                     function () {
  824.                                                                                         showHelpText(4,
  825.                                                                                         function () {
  826.                                                                                             setTimeout(
  827.                                                                                             function() {
  828.                                                                                                 hideHelpText(4, 
  829.                                                                                                 function () {
  830.                                                                                                     document.getElementById("new").src = "images/new_pressed.png";
  831.                                                                                                     setTimeout(
  832.                                                                                                     function() {
  833.                                                                                                         document.getElementById("new").src = "images/new_normal.png";
  834.                                                                                                         addNote();
  835.                                                                                                         
  836.                                                                                                         help_canvas_y_stretch = 2.5;
  837.                                                                                                         help_canvas_x_stretch = 6;
  838.                                                                                                         circleObject('screen',
  839.                                                                                                         function () {
  840.                                                                                                             var edit_div = document.getElementById("edit_div");
  841.                                                                                                             var sel=window.getSelection();
  842.                                                                                                             sel.setBaseAndExtent(edit_div.firstChild, 0, edit_div.lastChild, edit_div.lastChild.length);
  843.                                                                                                             sel.collapseToEnd();
  844.                                                                                                             
  845.                                                                                                             showHelpText(5,
  846.                                                                                                             function () {
  847.                                                                                                                 setTimeout(
  848.                                                                                                                 function() {
  849.                                                                                                                     hideHelpText(5, 
  850.                                                                                                                     function () {
  851.                                                                                                                         deleteNote();
  852.                                                                                                                         alert('yo');
  853.                                                                                                                         document.getElementById("help_circle_canvas").style.display = "none";
  854.                                                                                                                         setTimeout(
  855.                                                                                                                         function() {
  856.                                                                                                                             showHelpText(6,
  857.                                                                                                                             function () {
  858.                                                                                                                                 setTimeout(
  859.                                                                                                                                 function() {
  860.                                                                                                                                     hideHelpText(6, 
  861.                                                                                                                                     function () {
  862.                                                                                                                                         showHelpText(7,
  863.                                                                                                                                         function () {
  864.                                                                                                                                             setTimeout(
  865.                                                                                                                                             function() {
  866.                                                                                                                                                 hideHelpText(7, 
  867.                                                                                                                                                 function () {
  868.                                                                                                                                                         //document.getElementById("help_circle_canvas").style.display = "none";
  869.                                                                                                                                                         document.getElementById("cover").style.display = "none";
  870.                                                                                                                                                         toggleSettings();
  871.                                                                                                                                                 }); //hideHelpText 7
  872.                                                                                                                                             }, 3500);
  873.                                                                                                                                         }); //showHelpText 7
  874.                                                                                                                                     }); //hideHelpText 6
  875.                                                                                                                                 }, 3500);
  876.                                                                                                                             }, 100); //showHelpText 6
  877.                                                                                                                         }, 1000);
  878.                                                                                                                     }); //hideHelpText 5
  879.                                                                                                                 }, 3500);
  880.                                                                                                             }); //showHelpText 5            
  881.                                                                                                         }); //circleObject 'screen'
  882.                                                                                                     }, 500);
  883.                                                                                                 }); //hideHelpText 4
  884.                                                                                             }, 1500);
  885.                                                                                         });//showHelpText 4
  886.                                                                                     }); //circleObject 'new'
  887.                                                                                 });//hideHelpText 3
  888.                                                                             }, 3500);
  889.                                                                         });//circleObject 'delete'
  890.                                                                     });//showHelpText 3
  891.                                                                 });//hideHelpText 3
  892.                                                             }, 3000);
  893.                                                         });//howHelpText 3
  894.                                                     });//circleObject 'front'
  895.                                                 }, 1000);
  896.                                             }); //fade 'help_circle_canvas'
  897.                                         });//hideHelpText 1
  898.                                     }, 3500);
  899.                                 });//circleObject 'nickname_combo'
  900.                             });//showHelpText 1
  901.                         }, 1500);
  902.                     });//hideHelpText 0
  903.                 }, 3500);
  904.             });//circleObject 'screen'
  905.         });//showHelpText 0
  906.     }, 1000);
  907. }
  908.  
  909. function cancelHelp() {
  910.     help_running = false;
  911.     var help_text = document.getElementById("help_text");
  912.     
  913.     document.getElementById("help_circle_canvas").style.display = "none";
  914.     document.getElementById("cover").style.display = "none";
  915.     
  916.     var messages = help_text.getElementsByTagName("div");
  917.     for (var i=0; i < messages.length; i++)
  918.         messages[i].style.opacity = 0;
  919.         
  920.     help_text.style.display = "none";
  921. }
  922.  
  923. function circleObject(obj, callBack) {
  924.     if (!help_running) return false;
  925.     
  926.     if (obj == new String(obj)) obj = document.getElementById(obj);
  927.     
  928.     var help_context = help_canvas.getContext("2d");
  929.     
  930.     help_canvas.style.display = "block";
  931.     
  932.     help_canvas.style.top = (getOffsetTop(obj) - 15) + "px";
  933.     help_canvas.style.left = (getOffsetLeft(obj) - 15) + "px";
  934.     
  935.     help_context.lineWidth = 1; //help_canvas_pen;
  936.     help_context.lineJoin = "bevel";
  937.     help_context.lineCap = "round";
  938.     help_context.strokeStyle = "rgba(255, 0, 0, 0.8)";
  939.     
  940.     help_context.clearRect(0, 0, 300, 300);
  941.     help_context.save();
  942.     
  943.     help_context.scale(help_canvas_x_stretch, help_canvas_y_stretch);
  944.     
  945.     help_context.beginPath();
  946.     help_context.moveTo(5, 10);
  947.     
  948.     var i=0;
  949.     
  950.     var inter = setInterval(
  951.         function () {
  952.             //alert(i);
  953.             switch (i) {
  954.                 case 0:
  955.                     help_context.bezierCurveTo(5, 0, 20, 10, 20, 20);
  956.                     break;
  957.                 case 1:
  958.                     help_context.beginPath();
  959.                     help_context.moveTo(20, 10);
  960.                     help_context.bezierCurveTo(30, 20, 20, 30, 15, 20);
  961.                     break;
  962.                 case 2:
  963.                     help_context.beginPath();
  964.                     help_context.moveTo(20, 30);
  965.                     help_context.bezierCurveTo(10, 35, 5, 25, 5, 10);
  966.                     break;
  967.                 case 3:
  968.                     help_context.beginPath();
  969.                     help_context.moveTo(5, 25);
  970.                     help_context.bezierCurveTo(2, 7, 9, 10, 0, 0);
  971.                     break;
  972.                 //Default:
  973.                 case 4:
  974.                     clearInterval(inter);
  975.                     help_context.restore();
  976.                     if (callBack != null) callBack();
  977.                     break;
  978.             }
  979.             help_context.stroke();
  980.             i++;
  981.         }, 70);
  982. }
  983.  
  984. function showHelpText(which, callBack) {
  985.     if (!help_running) return false;
  986.     
  987.     var help_text = document.getElementById("help_text");
  988.     help_text.style.display = "block";
  989.     
  990.     var messages = help_text.getElementsByTagName("div");
  991.     if (which > 0) messages[which-1].style.opacity = 0;
  992.     
  993.     fade(messages[which], 0, 0.9, 
  994.         function() {
  995.             //setTimeout("help_text_shadow.paint()", 100);
  996.             callBack();
  997.         });
  998. }
  999.  
  1000. function hideHelpText(which, callBack) {
  1001.     if (!help_running) return false;
  1002.     
  1003.     var help_text = document.getElementById("help_text");
  1004.     
  1005.     var messages = help_text.getElementsByTagName("div");
  1006.     //if (which > 0) messages[which-1].style.opacity = 0;
  1007.     
  1008.     fade(messages[which], 0.9, 0, 
  1009.         function () {
  1010.             help_text.style.display = "none";
  1011.             callBack();
  1012.         });
  1013. }
  1014.  
  1015. var anim, help_canvas;
  1016.  
  1017. function init() {    
  1018.     anim = new Animation('saving_anim', 'async', 276, 0, 50);
  1019.     
  1020.     //set up the flip ('i') button:
  1021.     var bod = document.getElementsByTagName("body")[0];
  1022.     bod.addEventListener("mouseover", mouseOver, true);
  1023.     bod.addEventListener("mouseout", mouseOut, true);
  1024.     document.getElementById("flip").addEventListener("mouseup", infoMouseUp, false);    
  1025.  
  1026.     //and now set up our buttons:
  1027.     var buttons = document.getElementById("buttons").getElementsByTagName("img");
  1028.     for (var i=0; i < buttons.length; i++) {
  1029.         buttons[i].addEventListener("mousedown", 
  1030.             function(e) {
  1031.                 e = e.target;
  1032.                 
  1033.                 var src = getFileName(e.src);
  1034.                 src = src.split("_");
  1035.                 src = "images/" + src[0] + "_pressed.png";
  1036.                 
  1037.                 e.src = src;
  1038.             }, true);
  1039.             
  1040.         buttons[i].addEventListener("mouseup", 
  1041.             function(e) {
  1042.                 e = e.target;
  1043.                 
  1044.                 var src = getFileName(e.src);
  1045.                 src = src.split("_");
  1046.                 src = "images/" + src[0] + "_normal.png";
  1047.                 
  1048.                 e.src = src;
  1049.             }, true);
  1050.     }
  1051.  
  1052.     if (window.widget) {
  1053.         //here we get all the files, then display them and select the first one (probably the last one to be used, actually)
  1054.         notes_folder = expandTilde(notes_folder);
  1055.         
  1056.         if (!folderExists(notes_folder)) {
  1057.             widget.system("/bin/mkdir " + notes_folder, null);
  1058.         } else {
  1059.             getNotes();
  1060.         }
  1061.         
  1062.         toggleButtons("play");
  1063.         setUpScrollBar();
  1064.     }
  1065.     
  1066.     document.addEventListener("keydown", handleKeyPress, false);
  1067.     help_canvas = document.getElementById("help_circle_canvas");
  1068. }
  1069.  
  1070. window.onload = init;